package com.loyin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.et.mvc.JsonView;
import com.et.mvc.View;
import com.loyin.model.DownloadUrl;
import com.loyin.model.UserInfoVo;
import com.loyin.util.Page;

public class DownloadUrlController extends ApplicationController {
	private Logger log=Logger.getLogger(this.getClass());
	public void list(){
		UserInfoVo userinfovo=(UserInfoVo)session.getAttribute("user_session");
		StringBuffer sql = new StringBuffer(" userid = ? ");
		DownloadUrl msginfo=new DownloadUrl();
		try {
			String pagenostr=request.getParameter("pageNo");
			String pagesizestr=request.getParameter("pageSize");
			if(pagenostr==null||"".equals(pagenostr.trim())){
				pagenostr="1";
			}
			if(pagesizestr==null||"".equals(pagesizestr.trim())){
				pagesizestr=this.pagesize+"";
			}
			Page page=new Page(0,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),null);
			List<Object> paramlist=new ArrayList<Object>();
			paramlist.add(userinfovo.id);
			String websiteid=request.getParameter("websiteid");
			request.setAttribute("websiteid",websiteid);
			if(websiteid!=null&&!"".equals(websiteid.trim())){
				sql.append(" and websiteid = ? ");
				paramlist.add(websiteid.trim());
			}
			
			String title=request.getParameter("title");
			request.setAttribute("title",title);
			if(title!=null&&!"".equals(title.trim())){
				sql.append(" and title like ? ");
				paramlist.add("%"+title.trim()+"%");
			}
			List<DownloadUrl> list = (List<DownloadUrl>) msginfo.findAll(msginfo.getClass(), sql.toString(), paramlist.toArray(),null,page.getPageSize(),page.start);
			long count=msginfo.count(msginfo.getClass(),sql.toString(), paramlist.toArray());
			page=new Page(count,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),list);
			request.setAttribute("page",page);
		} catch (Exception e) {
			log.error("",e);
			request.setAttribute("stat", "E");
			request.setAttribute("msg", "访问出错");
		}
	}
	public View delete(){
		Map<String,Object> map=new HashMap<String,Object>();
		try{
			String[]ids=request.getParameterValues("ids");
			StringBuffer sql=new StringBuffer("id in (");
			if(ids!=null&&ids.length>0){
				for(String id:ids){
					sql.append(id);
					sql.append(",");
				}
			}
			sql.append("EE)");
			
			int res=DownloadUrl.deleteAll(DownloadUrl.class, sql.toString().replaceAll(",EE", "").replaceAll("EE", ""),null);
			if(res>0){
				map.put("stat","S");
				map.put("msg","删除成功");
			}else{
				map.put("stat","F");
				map.put("msg","删除失败");
			}
		}catch(Exception e){
			log.error("删除异常",e);
			map.put("stat","E");
			map.put("msg","删除异常");
		}
		return new JsonView(map);
	}
}
